package com.bckj.fastboot.mq.ons.retry;

import com.aliyun.openservices.ons.api.Action;
import com.aliyun.openservices.ons.api.ConsumeContext;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.MessageListener;
import com.bckj.fastboot.core.extra.retry.AbstractRetryConsumer;
import com.bckj.fastboot.core.extra.retry.RetryContext;
import com.bckj.fastboot.core.extra.retry.RetryProducer;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RequiredArgsConstructor
public class OnsRetryConsumer extends AbstractRetryConsumer implements MessageListener {
    private final RetryProducer retryProducer;

    @Override
    public Action consume(Message message, ConsumeContext consumeContext) {
        String body = new String(message.getBody());
        log.info("收到重试消息, messageId=" + message.getMsgID() + ", body=" + body);
        RetryContext retryContext = RetryContext.fromJson(body);
        int r = consume(retryContext);
        return r == 1 ? Action.CommitMessage : Action.ReconsumeLater;
    }

    @Override
    public RetryProducer getRetryProducer() {
        return retryProducer;
    }
}
